package datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/14.
 */
//创建数据源,只需要创建一次,然而每从里面取面就可以了
public class C3POUtils {
    //定义成员变量,其他方法才能使用
    public static ComboPooledDataSource cpds=null;
    //只需要创建一次?static 代码块,只加载一次
    static{
        //c3p0的数据库关联
        cpds=new ComboPooledDataSource();
        //设置与数据库的关联
        try {
            cpds.setDriverClass("com.mysql.jdbc.Driver");
            cpds.setJdbcUrl("jdbc:mysql://localhost:3307/test");
            cpds.setUser("root");
            cpds.setPassword("123456");
            //设置默认连接数
            cpds.setInitialPoolSize(5);
            cpds.setMaxPoolSize(15);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        //ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();
    }
public static Connection getConnection() throws SQLException {
        return cpds.getConnection();
}
}
